package falling 
{
	import org.flixel.*;
	/**
	 * ...
	 * @author morgan
	 */
	public class FallingState3 extends GameState
	{
		[Embed (source = "../../data/falling/3_bg.png")] private var img_bg:Class;
		[Embed (source = "../../data/falling/3_player.png")] private var img_player:Class;
		[Embed (source = "../../data/falling/3_cloud.png")] private var img_cloud:Class;
		
		public var bg:FlxSprite;
		public var player:FlxSprite;
		public var cloud:FlxSprite;
		public var cloudUnder:FlxGroup;
		public var cloudOver:FlxGroup;
		public var cloudT:Number = 0;
		
		public function FallingState3() 
		{
			
		}
		
		override public function create():void 
		{
			super.create();
			bg = new FlxSprite( -105, -85, img_bg);
			bg.scale.x = 0.6; bg.scale.y = 0.6;
			hazy.add(bg);
			
			cloudUnder = new FlxGroup();
			hazy.add(cloudUnder);
			
			player = new FlxSprite( 150, 50, img_player);
			player.scale.x = 0.3;
			player.scale.y = 0.3;
			hazy.add(player);
			
			cloudOver = new FlxGroup();
			hazy.add(cloudOver);			
			
			cloud = new FlxSprite(5, 5, img_cloud);
			cloud.alpha = 0;
			cloudOver.add(cloud);
		}
		
		override public function update():void 
		{
			super.update();
			if (time < 30)
			{
				if (bg.scale.x < 0.83)
				{
					bg.scale.x += FlxG.elapsed / 7;
					bg.scale.y += FlxG.elapsed / 7;
					player.scale.x += FlxG.elapsed / 4;
					player.scale.y += FlxG.elapsed / 4;
				}
				else
				{
					bg.scale.x += FlxG.elapsed / 50;
					bg.scale.y += FlxG.elapsed / 50;
				}
				
				player.acceleration.x = (FlxG.mouse.x - (player.x + player.width / 2)) / 10;
				player.acceleration.y = (FlxG.mouse.y - (player.y + player.height / 2)) / 10;
				
				if (cloud.alpha <= 0 && Math.random() < 0.01)
				{
					cloudT = 0;
					cloudUnder.add(cloud);
					cloudOver.remove(cloud);
					cloud.x = Math.random() * 400 - 200;
					cloud.y = Math.random() * 300 - 200;
					cloud.angle = Math.random() * 360;
				}
				if (cloudT < 1)
				{
					cloudT += FlxG.elapsed / 6;
					cloud.scale.x = cloud.scale.y = cloudT + 0.75;
					cloud.alpha = 1.0 - Math.abs(0.5 - cloudT) * 2;
					if (cloudT < 0.3 && cloudT + FlxG.elapsed >= 0.3)
					{
						cloudOver.add(cloud);
						cloudUnder.remove(cloud);
					}
				}
			}
			else
			{
				player.scale.x *= 1.0 - 0.4 * FlxG.elapsed;
				player.scale.y *= 1.0 - 0.4 * FlxG.elapsed;
				if (player.scale.x <= 0.025)
				{
					player.kill();
				}
				cloud.alpha -= FlxG.elapsed/3;
			}
			player.maxVelocity = new FlxPoint(60 * player.scale.x, 60 * player.scale.x);
		}
		
	}

}